home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 June / PersonalComputerWorld-June2009-CoverdiscCD.iso / Software / Freeware / Adobe AIR 1.5.1 / AdobeAIRInstaller.exe / setup.swf / scripts / mx / binding / PropertyWatcher.as < prev    next >
Encoding:
Text File  |  2009-02-12  |  5.0 KB  |  177 lines

  1. package mx.binding
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.IEventDispatcher;
  5.    import flash.utils.getQualifiedClassName;
  6.    import mx.core.EventPriority;
  7.    import mx.core.mx_internal;
  8.    import mx.events.PropertyChangeEvent;
  9.    import mx.utils.DescribeTypeCache;
  10.    
  11.    use namespace mx_internal;
  12.    
  13.    public class PropertyWatcher extends Watcher
  14.    {
  15.       mx_internal static const VERSION:String = "3.0.0.0";
  16.       
  17.       protected var propertyGetter:Function;
  18.       
  19.       private var parentObj:Object;
  20.       
  21.       protected var events:Object;
  22.       
  23.       private var useRTTI:Boolean;
  24.       
  25.       private var _propertyName:String;
  26.       
  27.       public function PropertyWatcher(param1:String, param2:Object, param3:Array, param4:Function = null)
  28.       {
  29.          super(param3);
  30.          _propertyName = param1;
  31.          this.events = param2;
  32.          this.propertyGetter = param4;
  33.          useRTTI = !param2;
  34.       }
  35.       
  36.       private function eventNamesToString() : String
  37.       {
  38.          var _loc2_:String = null;
  39.          var _loc1_:String = " ";
  40.          for(_loc2_ in events)
  41.          {
  42.             _loc1_ += _loc2_ + " ";
  43.          }
  44.          return _loc1_;
  45.       }
  46.       
  47.       override public function updateParent(param1:Object) : void
  48.       {
  49.          var _loc2_:String = null;
  50.          var _loc3_:BindabilityInfo = null;
  51.          if(Boolean(parentObj) && parentObj is IEventDispatcher)
  52.          {
  53.             for(_loc2_ in events)
  54.             {
  55.                parentObj.removeEventListener(_loc2_,eventHandler);
  56.             }
  57.          }
  58.          if(param1 is Watcher)
  59.          {
  60.             parentObj = param1.value;
  61.          }
  62.          else
  63.          {
  64.             parentObj = param1;
  65.          }
  66.          if(parentObj)
  67.          {
  68.             if(useRTTI)
  69.             {
  70.                events = {};
  71.                if(parentObj is IEventDispatcher)
  72.                {
  73.                   _loc3_ = DescribeTypeCache.describeType(parentObj).bindabilityInfo;
  74.                   events = _loc3_.getChangeEvents(_propertyName);
  75.                   if(objectIsEmpty(events))
  76.                   {
  77.                      trace("warning: unable to bind to property \'" + _propertyName + "\' on class \'" + getQualifiedClassName(parentObj) + "\'");
  78.                   }
  79.                   else
  80.                   {
  81.                      addParentEventListeners();
  82.                   }
  83.                }
  84.                else
  85.                {
  86.                   trace("warning: unable to bind to property \'" + _propertyName + "\' on class \'" + getQualifiedClassName(parentObj) + "\' (class is not an IEventDispatcher)");
  87.                }
  88.             }
  89.             else if(parentObj is IEventDispatcher)
  90.             {
  91.                addParentEventListeners();
  92.             }
  93.          }
  94.          wrapUpdate(updateProperty);
  95.       }
  96.       
  97.       private function objectIsEmpty(param1:Object) : Boolean
  98.       {
  99.          var _loc2_:String = null;
  100.          var _loc3_:int = 0;
  101.          var _loc4_:* = param1;
  102.          for(_loc2_ in _loc4_)
  103.          {
  104.             return false;
  105.          }
  106.          return true;
  107.       }
  108.       
  109.       override protected function shallowClone() : Watcher
  110.       {
  111.          return new PropertyWatcher(_propertyName,events,listeners,propertyGetter);
  112.       }
  113.       
  114.       private function traceInfo() : String
  115.       {
  116.          return "Watcher(" + getQualifiedClassName(parentObj) + "." + _propertyName + "): events = [" + eventNamesToString() + (useRTTI ? "] (RTTI)" : "]");
  117.       }
  118.       
  119.       public function get propertyName() : String
  120.       {
  121.          return _propertyName;
  122.       }
  123.       
  124.       private function addParentEventListeners() : void
  125.       {
  126.          var _loc1_:String = null;
  127.          for(_loc1_ in events)
  128.          {
  129.             if(_loc1_ != "__NoChangeEvent__")
  130.             {
  131.                parentObj.addEventListener(_loc1_,eventHandler,false,EventPriority.BINDING,true);
  132.             }
  133.          }
  134.       }
  135.       
  136.       private function updateProperty() : void
  137.       {
  138.          if(parentObj)
  139.          {
  140.             if(_propertyName == "this")
  141.             {
  142.                value = parentObj;
  143.             }
  144.             else if(propertyGetter != null)
  145.             {
  146.                value = propertyGetter.apply(parentObj,[_propertyName]);
  147.             }
  148.             else
  149.             {
  150.                value = parentObj[_propertyName];
  151.             }
  152.          }
  153.          else
  154.          {
  155.             value = null;
  156.          }
  157.          updateChildren();
  158.       }
  159.       
  160.       public function eventHandler(param1:Event) : void
  161.       {
  162.          var _loc2_:Object = null;
  163.          if(param1 is PropertyChangeEvent)
  164.          {
  165.             _loc2_ = PropertyChangeEvent(param1).property;
  166.             if(_loc2_ != _propertyName)
  167.             {
  168.                return;
  169.             }
  170.          }
  171.          wrapUpdate(updateProperty);
  172.          notifyListeners(events[param1.type]);
  173.       }
  174.    }
  175. }
  176.  
  177.